package com.wei.c.widget;

import android.animation.Animator;
import android.animation.AnimatorInflater;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.RelativeLayout;

import com.wei.c.lib.R;
import com.wei.c.widget.LockView.OnLockSlideListener;

public class UnlockSlideView extends RelativeLayout implements OnLockSlideListener {
    private static final boolean DEBUG = false;
    private static final String TAG = DEBUG ? "UnlockSlideView" : UnlockSlideView.class.getSimpleName();

    private ImageView mAnimRing, mAnimArrowLeft, mAnimArrowLeft1, mAnimArrowRight, mAnimArrowRight1;
    private LockView mLock;
    private ImageView mUnlockLeft, mUnlockRight;
    private OnLockSlideListener mOnLockSlideListener;

    public UnlockSlideView(Context context) {
        super(context);
        init(context);
    }

    public UnlockSlideView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public UnlockSlideView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);
    }

    private void init(Context context) {
        final int dp48 = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 48, getResources().getDisplayMetrics());

        mAnimRing = new ImageView(context);
        mAnimRing.setScaleType(ScaleType.CENTER);
        mAnimRing.setClickable(false);
        mAnimRing.setFocusable(false);
        mAnimRing.setFocusableInTouchMode(false);
        mAnimRing.setBackgroundResource(R.drawable.lock_btn_ring_s);
        mAnimRing.setId(0x00f0f0f000);
        final LayoutParams animRingLp = new LayoutParams(dp48, dp48);
        animRingLp.addRule(CENTER_IN_PARENT);
        addView(mAnimRing, animRingLp);

        mAnimArrowLeft = new ImageView(context);
        mAnimArrowLeft.setScaleType(ScaleType.CENTER);
        mAnimArrowLeft.setClickable(false);
        mAnimArrowLeft.setFocusable(false);
        mAnimArrowLeft.setFocusableInTouchMode(false);
        //mAnimArrowLeft.setImageResource(R.drawable.unlock_arrow_left);
        mAnimArrowLeft.setId(0x00f0f0f001);
        final LayoutParams animArrowLeftLp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        animArrowLeftLp.addRule(CENTER_VERTICAL);
        animArrowLeftLp.addRule(LEFT_OF, mAnimRing.getId());
        animArrowLeftLp.rightMargin = dp48 / 6;

        mAnimArrowLeft1 = new ImageView(context);
        mAnimArrowLeft1.setScaleType(ScaleType.CENTER);
        mAnimArrowLeft1.setClickable(false);
        mAnimArrowLeft1.setFocusable(false);
        mAnimArrowLeft1.setFocusableInTouchMode(false);
        //mAnimArrowLeft1.setImageResource(R.drawable.unlock_arrow_left);
        mAnimArrowLeft1.setId(0x00f0f0f002);
        final LayoutParams animArrowLeft1Lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        animArrowLeft1Lp.addRule(CENTER_VERTICAL);
        animArrowLeft1Lp.addRule(LEFT_OF, mAnimArrowLeft.getId());
        addView(mAnimArrowLeft1, animArrowLeft1Lp);
        //mAnimArrowLeft1依赖于mAnimArrowLeft, mAnimArrowLeft必须在后面add，否则mAnimArrowLeft1将不能正确布局
        addView(mAnimArrowLeft, animArrowLeftLp);

        mAnimArrowRight = new ImageView(context);
        mAnimArrowRight.setScaleType(ScaleType.CENTER);
        mAnimArrowRight.setClickable(false);
        mAnimArrowRight.setFocusable(false);
        mAnimArrowRight.setFocusableInTouchMode(false);
        //mAnimArrowRight.setImageResource(R.drawable.unlock_arrow_right);
        mAnimArrowRight.setId(0x00f0f0f003);
        final LayoutParams animArrowRightLp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        animArrowRightLp.addRule(CENTER_VERTICAL);
        animArrowRightLp.addRule(RIGHT_OF, mAnimRing.getId());
        animArrowRightLp.leftMargin = dp48 / 6;

        mAnimArrowRight1 = new ImageView(context);
        mAnimArrowRight1.setScaleType(ScaleType.CENTER);
        mAnimArrowRight1.setClickable(false);
        mAnimArrowRight1.setFocusable(false);
        mAnimArrowRight1.setFocusableInTouchMode(false);
        //mAnimArrowRight1.setImageResource(R.drawable.unlock_arrow_right);
        mAnimArrowRight1.setId(0x00f0f0f004);
        final LayoutParams animArrowRight1Lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        animArrowRight1Lp.addRule(CENTER_VERTICAL);
        animArrowRight1Lp.addRule(RIGHT_OF, mAnimArrowRight.getId());
        addView(mAnimArrowRight1, animArrowRight1Lp);
        addView(mAnimArrowRight, animArrowRightLp);

        final FrameLayout slide = new FrameLayout(context);
        final LayoutParams slideLp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);

        mLock = new LockView(context);
        final FrameLayout.LayoutParams lockLp = new FrameLayout.LayoutParams(dp48, dp48);
        lockLp.gravity = Gravity.CENTER;
        slide.addView(mLock, lockLp);
        addView(slide, slideLp);

        final int margin = dp48 / 4;
        mUnlockLeft = new ImageView(context);
        mUnlockLeft.setScaleType(ScaleType.CENTER);
        mUnlockLeft.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                startArrowAnim(false);
                startUnlockLeftAnim();
            }
        });
        final LayoutParams unlockLeftLp = new LayoutParams(dp48, dp48);
        unlockLeftLp.leftMargin = margin;
        unlockLeftLp.addRule(ALIGN_PARENT_LEFT);
        unlockLeftLp.addRule(CENTER_VERTICAL);
        //mUnlockLeft.setImageResource(R.drawable.unlock_to_news);
        addView(mUnlockLeft, unlockLeftLp);

        mUnlockRight = new ImageView(context);
        mUnlockRight.setScaleType(ScaleType.CENTER);
        mUnlockRight.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                startArrowAnim(false);
                startUnlockRightAnim();
            }
        });
        final LayoutParams unlockRightLp = new LayoutParams(dp48, dp48);
        unlockRightLp.rightMargin = margin;
        unlockRightLp.addRule(ALIGN_PARENT_RIGHT);
        unlockRightLp.addRule(CENTER_VERTICAL);
        mUnlockRight.setImageResource(R.drawable.unlock);
        addView(mUnlockRight, unlockRightLp);

        mLock.setUnlockIconCenterToBorder(margin + dp48 / 2);
        mLock.setOnLockSlideListener(this);
        resetUnlockView();
    }

    @Override
    protected void onWindowVisibilityChanged(int visibility) {
        super.onWindowVisibilityChanged(visibility);
        if (DEBUG) Log.e(TAG, "onWindowVisibilityChanged----visibility:" + visibility);
    }

    @Override
    public void onWindowFocusChanged(boolean hasWindowFocus) {
        super.onWindowFocusChanged(hasWindowFocus);
        if (DEBUG) Log.e(TAG, "onWindowFocusChanged----hasWindowFocus:" + hasWindowFocus);
        //最准确，即使下拉状态栏列表也能正确回调，而onWindowVisibilityChanged仍然显示为visible
        if (hasWindowFocus) startArrowAnim(true);
    }

    @Override
    public void onTouch() {
        hideArrow();
        if (mOnLockSlideListener != null) mOnLockSlideListener.onTouch();
    }

    @Override
    public void onAdsorbEnter(boolean trueLeftFalseRight) {
        if (trueLeftFalseRight) {
            startUnlockLeftAnim();
        } else {
            startUnlockRightAnim();
        }
        if (mOnLockSlideListener != null) mOnLockSlideListener.onAdsorbEnter(trueLeftFalseRight);
    }

    @Override
    public void onAdsorbExit(boolean trueLeftFalseRight) {
        if (trueLeftFalseRight) {
            startUnlockLeftAnim();
        } else {
            startUnlockRightAnim();
        }
        if (mOnLockSlideListener != null) mOnLockSlideListener.onAdsorbExit(trueLeftFalseRight);
    }

    @Override
    public void onScrollTo(int scrollTo) {
        /*if (scrollTo == 0) {
            resetUnlockView();
        } else if (scrollTo < 0) {
            if (mUnlockLeft.getVisibility() != View.INVISIBLE) {
                mUnlockLeft.setVisibility(View.INVISIBLE);
                stopUnlockLeftAnim();
            }
            if (mUnlockRight.getVisibility() != View.VISIBLE) {
                mUnlockRight.setVisibility(View.VISIBLE);
                startUnlockRightAnim();
            }
        } else {
            if (mUnlockLeft.getVisibility() != View.VISIBLE) {
                mUnlockLeft.setVisibility(View.VISIBLE);
                startUnlockLeftAnim();
            }
            if (mUnlockRight.getVisibility() != View.INVISIBLE) {
                mUnlockRight.setVisibility(View.INVISIBLE);
                stopUnlockRightAnim();
            }
        }*/
        if (mOnLockSlideListener != null) mOnLockSlideListener.onScrollTo(scrollTo);
    }

    @Override
    public void onScrollFinish(int direction) {
        reset();
        if (mOnLockSlideListener != null) mOnLockSlideListener.onScrollFinish(direction);
    }

    @Override
    public void onResetAnimEnd() {
        showArrow();
        if (mOnLockSlideListener != null) mOnLockSlideListener.onResetAnimEnd();
    }

    public void setActionDrawableId(int drawableId) {
        mUnlockLeft.setImageResource(drawableId);
    }

    public void showArrowAnim() {
        if (isArrowShown()) startArrowAnim(false);
    }

    public boolean isArrowShown() {
        return mAnimRing.isShown();
    }

    public void reset() {
        mLock.reset();
        resetUnlockView();
    }

    public void setOnLockSlideListener(OnLockSlideListener l) {
        mOnLockSlideListener = l;
    }

    private void resetUnlockView() {
        /*if (mUnlockLeft.getVisibility() != View.INVISIBLE) {
            mUnlockLeft.setVisibility(View.INVISIBLE);
            stopUnlockLeftAnim();
        }
        if (mUnlockRight.getVisibility() != View.INVISIBLE) {
            mUnlockRight.setVisibility(View.INVISIBLE);
            stopUnlockRightAnim();
        }*/
    }

    private Animator mUnlockLeftAnimator;
    private void startUnlockLeftAnim() {
        if (mUnlockLeftAnimator == null) {
            mUnlockLeftAnimator = AnimatorInflater.loadAnimator(getContext(), R.animator.unlock_btn_show);
            mUnlockLeftAnimator.setTarget(mUnlockLeft);
        }
        mUnlockLeftAnimator.cancel();
        mUnlockLeftAnimator.start();
    }

    private void stopUnlockLeftAnim() {
        if (mUnlockLeftAnimator != null) mUnlockLeftAnimator.cancel();
    }

    private Animator mUnlockRightAnimator;
    private void startUnlockRightAnim() {
        if (mUnlockRightAnimator == null) {
            mUnlockRightAnimator = AnimatorInflater.loadAnimator(getContext(), R.animator.unlock_btn_show);
            mUnlockRightAnimator.setTarget(mUnlockRight);
        }
        mUnlockRightAnimator.cancel();
        mUnlockRightAnimator.start();
    }

    private void stopUnlockRightAnim() {
        if (mUnlockRightAnimator != null) mUnlockRightAnimator.cancel();
    }

    private void showArrow() {
        if (mAnimRing.getVisibility() != View.VISIBLE) {
            mAnimRing.setVisibility(View.VISIBLE);
            mAnimArrowLeft.setVisibility(View.VISIBLE);
            mAnimArrowLeft1.setVisibility(View.VISIBLE);
            mAnimArrowRight.setVisibility(View.VISIBLE);
            mAnimArrowRight1.setVisibility(View.VISIBLE);
        }
    }

    private Animator mRingAnimator, mArrowLeftAnimator, mArrowLeft1Animator, mArrowRightAnimator, mArrowRight1Animator;
    private void startArrowAnim(boolean cancel) {
        showArrow();
        if (mRingAnimator == null) {
            //mRingAnimator = AnimatorInflater.loadAnimator(getContext(), R.animator.lock_arrow_ring);
            mRingAnimator.setTarget(mAnimRing);
            //mArrowLeftAnimator = AnimatorInflater.loadAnimator(getContext(), R.animator.lock_arrow);
            mArrowLeftAnimator.setTarget(mAnimArrowLeft);
            //mArrowLeft1Animator = AnimatorInflater.loadAnimator(getContext(), R.animator.lock_arrow_1);
            mArrowLeft1Animator.setTarget(mAnimArrowLeft1);
            //mArrowRightAnimator = AnimatorInflater.loadAnimator(getContext(), R.animator.lock_arrow);
            mArrowRightAnimator.setTarget(mAnimArrowRight);
            //mArrowRight1Animator = AnimatorInflater.loadAnimator(getContext(), R.animator.lock_arrow_1);
            mArrowRight1Animator.setTarget(mAnimArrowRight1);
        }
        if (cancel) {
            mRingAnimator.cancel();
            mArrowLeftAnimator.cancel();
            mArrowLeft1Animator.cancel();
            mArrowRightAnimator.cancel();
            mArrowRight1Animator.cancel();
            mRingAnimator.start();
            mArrowLeftAnimator.start();
            mArrowLeft1Animator.start();
            mArrowRightAnimator.start();
            mArrowRight1Animator.start();
        } else if (!mRingAnimator.isStarted()) {
            mRingAnimator.start();
            mArrowLeftAnimator.start();
            mArrowLeft1Animator.start();
            mArrowRightAnimator.start();
            mArrowRight1Animator.start();
        }
    }

    private void hideArrow() {
        stopArrowAnim();
        if (mAnimRing.getVisibility() != View.INVISIBLE) {
            mAnimRing.setVisibility(View.INVISIBLE);
            mAnimArrowLeft.setVisibility(View.INVISIBLE);
            mAnimArrowLeft1.setVisibility(View.INVISIBLE);
            mAnimArrowRight.setVisibility(View.INVISIBLE);
            mAnimArrowRight1.setVisibility(View.INVISIBLE);
        }
    }

    private void stopArrowAnim() {
        if (mRingAnimator != null) {
            mRingAnimator.end();
            mArrowLeftAnimator.end();
            mArrowLeft1Animator.end();
            mArrowRightAnimator.end();
            mArrowRight1Animator.end();
        }
    }
}
